package cn.itsource.org.service.impl;

import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.org.vo.EmployeeRole;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    @Transactional
    public void add(Employee employee) {
        // 添加员工表
//        String salt = StrUtils.getComplexRandomString(32);
//        String passwordRandom = StrUtils.getComplexRandomString(6);
        // 密码是 1
        String password = MD5Utils.encrypByMd5("z9HQpuKqIdbNN7uIPQm53W2bzxs0f7Ei"+"1");
        employee.setSalt("z9HQpuKqIdbNN7uIPQm53W2bzxs0f7Ei");
        employee.setPassword(password);
        employeeMapper.add(employee);
        // 添加logininfo表
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfoMapper.add(logininfo);
        // 更改 emp的loginifo的id
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.update(employee);

        // 添加用户 角色 中间表
        employeeMapper.addEmployeeRole(employee.getId(),employee.getRoles());
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
        // 添加logininfo表
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfoMapper.update(logininfo);

        // 删除 用户 角色中间表
        employeeMapper.deleteByEmpId(employee.getId());

        // 添加用户 角色 中间表
        employeeMapper.addEmployeeRole(employee.getId(),employee.getRoles());

    }

    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {
        // 去除 keyword的前后空格
        if (!StringUtils.isEmpty(query.getKeyword())) {
            query.setKeyword(query.getKeyword().trim());
        }
        // 查询数量
        Integer count = employeeMapper.queryByCount(query);
        // 查询数据
        List<Employee> ts = employeeMapper.queryByPage(query);
        // 查询角色id集合
        List<Long> eIds = ts.stream().map(Employee::getId).collect(Collectors.toList());
        List<EmployeeRole>  employeeRoles = employeeMapper.findRoleIdsByEIds(eIds);
        Map<Long, List<Long>> employeeRolesMap = employeeRoles.stream()
                .collect(Collectors.groupingBy(EmployeeRole::getEmployeeId,
                        Collectors.mapping(EmployeeRole::getRoleId, Collectors.toList())));
        ts.forEach(t->{
            List<Long> rolesId = employeeRolesMap.get(t.getId());
            if (!CollectionUtils.isEmpty(rolesId)){
                t.setRoles(rolesId);
            }
        });

        return new PageList<Employee>(count,ts);
    }

    @Override
    public Employee selectByLogininfoId(Long id) {
        return employeeMapper.selectByLogininfoId(id);
    }

    @Override
    public List<Employee> selectByShopId(Long shopId) {
        List<Employee> employees = employeeMapper.selectByShopId(shopId);
        return employees;
    }
}
